package com.ehotting.edsta.circuitDesign.gen;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ehotting.eaf.core.domain.R;
import com.ehotting.eaf.core.utils.StringUtils;
import com.ehotting.eaf.redis.service.RedisService;
import com.ehotting.edsta.circuitDesign.bean.CircuitPropertyBean;
import com.ehotting.edsta.circuitDesign.bean.Constant;
import com.ehotting.edsta.circuitDesign.domain.EsCircuit;
import com.ehotting.edsta.circuitDesign.domain.EsCircuitCodeData;
import com.ehotting.edsta.circuitDesign.enums.CircuitTypeEnum;
import com.ehotting.edsta.circuitDesign.enums.DataTypeEnum;
import com.ehotting.edsta.circuitDesign.gen.element.helper.ElementDataHelper;
import com.ehotting.edsta.circuitDesign.service.IEsCircuitCodeDataService;
import com.ehotting.edsta.circuitDesign.service.IEsCircuitService;
import com.ehotting.edsta.circuitDesign.service.impl.ExcelToMapListListThreadPool;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class PropertyFilterHelper {

    private static Logger logger = LoggerFactory.getLogger(ElementDataHelper.class);
    @Autowired
    private IEsCircuitService esCircuitService;

    @Autowired
    private IEsCircuitCodeDataService esCircuitCodeDataService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private PropertyTipPackageHelper propertyTipPackageHelper;

    @Data
    class FilterOutcome{
        private boolean bContinue = true;
        private String msg;
    }

    /**
     * 从数据库加载所有电路的属性
     */
    public void loadAllProperty4DB(){
        CircuitPropertyBean circuitPropertyBean = new CircuitPropertyBean();

        // 三种类型的电路集合
        List<CircuitPropertyBean.CircuitBean> senseCircuitListBean = new ArrayList<>();
        List<CircuitPropertyBean.CircuitBean> controllerCircuitListBean = new ArrayList<>();
        List<CircuitPropertyBean.CircuitBean> powerCircuitListBean = new ArrayList<>();

        // 加载所有电路
        List<EsCircuit> circuitList = esCircuitService.selectEsCircuitList(null);
        ArrayList<List<EsCircuit>> circuitLists = new ArrayList<>();
        double listSize = 500.0;
        int ceil = (int) Math.ceil(circuitList.size() / listSize);
        for(int i=1;i<=ceil;i++){
            int end = i*listSize > circuitList.size() ? circuitList.size() : (int) (i * listSize);
            int start = (int) ((i-1)*listSize);
            circuitLists.add(circuitList.subList(start,end));
        }

        ExcelToMapListListThreadPool<List<EsCircuit>> threadPool = new ExcelToMapListListThreadPool<List<EsCircuit>>() {
            @Override
            public void task(List<EsCircuit> o) {
                for (EsCircuit esCircuit : o){

                    CircuitPropertyBean.CircuitBean circuitBean = new CircuitPropertyBean.CircuitBean();

                    // 加载电路属性
                    Map<String, CircuitPropertyBean.PropertyBean> propertyMap = new HashMap<>();
                    List<EsCircuitCodeData> circuitCodeDataList = esCircuitCodeDataService.findByCircuitId(esCircuit.getId());
                    for (EsCircuitCodeData circuitCodeData : circuitCodeDataList){
                        CircuitPropertyBean.PropertyBean propertyBean = new CircuitPropertyBean.PropertyBean();
                        propertyBean.setCode(circuitCodeData.getCircuitCodeDataTemplate().getCode());
                        propertyBean.setName(circuitCodeData.getCircuitCodeDataTemplate().getName());
                        propertyBean.setValue(circuitCodeData.getValue());
                        propertyBean.setDataType(circuitCodeData.getCircuitCodeDataTemplate().getDataType().getCode());
                        propertyMap.put(circuitCodeData.getCircuitCodeDataTemplate().getCode(), propertyBean);
                    }

                    // 加载数字电路的输入类型属性
                    if (esCircuit.getCircuitType().getCircuitType() == CircuitTypeEnum.SENSOR){
                        CircuitPropertyBean.PropertyBean propertyBean = new CircuitPropertyBean.PropertyBean();
                        propertyBean.setCode(CircuitPropertyBean.PropertyEnum.SIGNALTYPE.getCode());
                        propertyBean.setName(esCircuit.getSignalType().getName());
                        propertyBean.setValue(String.valueOf(esCircuit.getSignalType().getValue()));
                        propertyBean.setDataType(DataTypeEnum.LONG.getCode());
                        propertyMap.put(CircuitPropertyBean.PropertyEnum.SIGNALTYPE.getCode(), propertyBean);
                    }


                    circuitBean.setId(esCircuit.getId());
                    circuitBean.setName(esCircuit.getName());
                    circuitBean.setType(esCircuit.getCircuitType().getCircuitType().getCode());
                    circuitBean.setPropertyMap(propertyMap);

                    switch (esCircuit.getCircuitType().getCircuitType()){

                        case SENSOR:
                            senseCircuitListBean.add(circuitBean);
                            break;

                        case CONTROLLER:
                            controllerCircuitListBean.add(circuitBean);
                            break;

                        case POWER:
                            powerCircuitListBean.add(circuitBean);
                            break;
                    }

                }
            }
        };
        threadPool.execute(circuitLists);

        circuitPropertyBean.setSensorCircuitListBean(senseCircuitListBean);
        circuitPropertyBean.setControllerCircuitList(controllerCircuitListBean);
        circuitPropertyBean.setPowerCircuitList(powerCircuitListBean);


        redisService.setCacheObject(Constant.CIRCUIT_PROP_CACHE_KEY_PREFIX + "allProperty4DB", circuitPropertyBean);

    }

    /**
     * 过滤电路
     */
    public R filterCircuit(String param,String mainProjectId){


        List<CircuitPropertyBean.Data> dataList = JSONArray.parseArray(param, CircuitPropertyBean.Data.class);

//        LinkedHashMap map = propertyTipPackageHelper.packageProperty(circuitPropertyBeanCopy,mainProjectId,data.getPage());
//        logger.info("page:{} \t paramTips response:{}",data.getPage(), JSONObject.toJSONString(map));
//        if(dataList.size() == 1){
//            return R.ok(JSONObject.toJSONString(map));
//        }

        CircuitPropertyBean circuitPropertyBean = redisService.getCacheObject(Constant.CIRCUIT_PROP_CACHE_KEY_PREFIX + "allProperty4DB");

        if (circuitPropertyBean == null){
            return R.fail("allProperty4DB in redis is null");
        }
        // 电路没有属性，则删除此电路
        propNullRemove(circuitPropertyBean);
        String jsonString = JSONObject.toJSONString(circuitPropertyBean);
        FilterOutcome filterOutcome = new FilterOutcome();
        for(CircuitPropertyBean.Data data : dataList){
            CircuitPropertyBean circuitPropertyBeanCopy = JSONObject.parseObject(jsonString,CircuitPropertyBean.class);
            // 质量等级过滤
            qualityGradeFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 总线通信类型过滤
            CpuInterfaceFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 电压等级过滤
            inVoltageFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 处理器类型过滤
            controllerFilter(circuitPropertyBeanCopy,data,filterOutcome);
            //fpga筛选
            fpgaFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 最大功率过滤
            maxPowerFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 采集类型过滤
            signalTypeFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 最小频率-信号带宽过滤
            fMinFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 最大频率-信号带宽过滤
            fMaxFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 最小输入范围-数据范围过滤
            inputMinRangeFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 最大输入范围-数据范围过滤
            inputMaxRangeFilter(circuitPropertyBeanCopy,data,filterOutcome);
            //采样率过滤
            samplingFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 采样分辨率过滤
            adcSamplingBitFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 数字信号输入类型过滤
            inTypeFilter(circuitPropertyBeanCopy,data,filterOutcome);
            // 输入信号电压过滤
            inputVoltageRangeFilter(circuitPropertyBeanCopy,data,filterOutcome);

            // 只发送一页为获取输入限制，需返回打包数据
            if(!filterOutcome.bContinue){
                break;
            }
            LinkedHashMap map = propertyTipPackageHelper.packageProperty(circuitPropertyBeanCopy,mainProjectId,data.getPage());
            logger.info("page:{} \t paramTips response:{}",data.getPage(), JSONObject.toJSONString(map));
            if(dataList.size() == 1){
                return R.ok(JSONObject.toJSONString(map));
            }
        }
        return filterOutcome.bContinue ?  R.okMsg("参数校验通过") : R.fail(filterOutcome.getMsg());

    }

    /**
     * 采样率筛选
     */
    public void samplingFilter(CircuitPropertyBean circuitPropertyBean,
                           CircuitPropertyBean.Data data,
                           FilterOutcome filterOutcome){

        if (StringUtils.isEmpty(data.getSignalType())
                || StringUtils.isEmpty(data.getSample())
                || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            // 采样率（特殊处理，为空的不筛选）
            String sampleCode = CircuitPropertyBean.PropertyEnum.SAMPLE.getCode();
            if (circuitBean.getPropertyMap() != null && circuitBean.getPropertyMap().get(sampleCode) != null){
                if (!filterPropertyMaxValueRange(circuitBean, data.getSample(), sampleCode)){
                    circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
                }
            }
        }
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前采率不符合匹配要求");
        }
    }

    /**
     * fpga筛选
     */
    public void fpgaFilter(CircuitPropertyBean circuitPropertyBean,
                                   CircuitPropertyBean.Data data,
                                   FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getController())
                || !filterOutcome.bContinue
                || !data.getController().toUpperCase().equals("FPGA")) {
            return;
        }
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            String value = circuitBean.getPropertyMap().get(CircuitPropertyBean.PropertyEnum.OUTPUTTYPE.getCode()).getValue();
            if(!value.contains("1")){
                circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
            }
        }
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\tFPGA控制器类型不符合匹配要求");
        }
    }

    /**
     * 电路没有属性，则删除此电路
     */
    public void propNullRemove(CircuitPropertyBean circuitPropertyBean){
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean sensorCircuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            if (sensorCircuitBean.getPropertyMap() == null) {
                circuitPropertyBean.getSensorCircuitListBean().remove(sensorCircuitBean);
            }
        }
        for (int i = circuitPropertyBean.getControllerCircuitList().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean controllCircuitBean = circuitPropertyBean.getControllerCircuitList().get(i);
            if (controllCircuitBean.getPropertyMap() == null) {
                circuitPropertyBean.getControllerCircuitList().remove(controllCircuitBean);
            }
        }
        for (int i = circuitPropertyBean.getPowerCircuitList().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean powerCircuitBean = circuitPropertyBean.getPowerCircuitList().get(i);
            if (powerCircuitBean.getPropertyMap() == null) {
                circuitPropertyBean.getPowerCircuitList().remove(powerCircuitBean);
            }
        }
    }

    /**
     * 质量等级筛选
     */
    public void qualityGradeFilter(CircuitPropertyBean circuitPropertyBean,
                                      CircuitPropertyBean.Data data,
                                      FilterOutcome filterOutcome){

        if (StringUtils.isEmpty(data.getQualitygrade())
                || data.getQualitygrade().equals("Consumer")
                || !filterOutcome.bContinue){
            return;
        }

        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean sensorCircuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            CircuitPropertyBean.PropertyBean signalTypePropertyBean = sensorCircuitBean.getPropertyMap().get(CircuitPropertyBean.PropertyEnum.SIGNALTYPE.getCode());
            // 电路没有采集类型，则删除此电路
            if (signalTypePropertyBean == null || StringUtils.isEmpty(signalTypePropertyBean.getValue())) {
                circuitPropertyBean.getSensorCircuitListBean().remove(sensorCircuitBean);
                continue;
            }
            // 质量等级（特殊处理）
            if (!filterPropertyQualityGrade(sensorCircuitBean, data.getQualitygrade(), CircuitPropertyBean.PropertyEnum.QUALITYGRADE.getCode())) {
                circuitPropertyBean.getSensorCircuitListBean().remove(sensorCircuitBean);
            }
        }

        for (int i = circuitPropertyBean.getControllerCircuitList().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean controllCircuitBean = circuitPropertyBean.getControllerCircuitList().get(i);
            // 质量等级（特殊处理）
            if (!filterPropertyQualityGrade(controllCircuitBean, data.getQualitygrade(), CircuitPropertyBean.PropertyEnum.QUALITYGRADE.getCode())) {
                circuitPropertyBean.getControllerCircuitList().remove(controllCircuitBean);
            }
        }

        for (int i = circuitPropertyBean.getPowerCircuitList().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean powerCircuitBean = circuitPropertyBean.getPowerCircuitList().get(i);
            // 质量等级（特殊处理）
            if (!filterPropertyQualityGrade(powerCircuitBean, data.getQualitygrade(), CircuitPropertyBean.PropertyEnum.QUALITYGRADE.getCode())) {
                circuitPropertyBean.getPowerCircuitList().remove(powerCircuitBean);
            }
        }

        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0
                || circuitPropertyBean.getControllerCircuitList().size() == 0
                || circuitPropertyBean.getPowerCircuitList().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前质量等级参数不符合要求");
        }
    }

    /**
     * 总线通信类型筛选
     */
    public void CpuInterfaceFilter(CircuitPropertyBean circuitPropertyBean,
                                      CircuitPropertyBean.Data data,
                                      FilterOutcome filterOutcome){
            if (StringUtils.isEmpty(data.getCpuInterface()) || !filterOutcome.bContinue) {
                return;
            }
            for (int i = circuitPropertyBean.getControllerCircuitList().size()-1; i >= 0; i--) {
                CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getControllerCircuitList().get(i);
                // 总线通信类型
                String replace = data.getCpuInterface().replace("-M", "").replace("-S", "");
                if (!filterPropertyEQ(circuitBean,replace, CircuitPropertyBean.PropertyEnum.CPUINTERFACE.getCode())){
                    circuitPropertyBean.getControllerCircuitList().remove(circuitBean);
                }
            }
            if(circuitPropertyBean.getControllerCircuitList().size() == 0){
                filterOutcome.setBContinue(false);
                filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前总线通信类型不符合匹配要求");
            }

    }

    /**
     * 电压等级筛选
     */
    public void inVoltageFilter(CircuitPropertyBean circuitPropertyBean,
                                   CircuitPropertyBean.Data data,
                                   FilterOutcome filterOutcome){
            if (StringUtils.isEmpty(data.getInVoltage()) || !filterOutcome.bContinue) {
                return;
            }
            for (int i = circuitPropertyBean.getPowerCircuitList().size()-1; i >= 0; i--) {
                CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getPowerCircuitList().get(i);
                // 电压等级
                if (!filterPropertyContain(circuitBean, data.getInVoltage(), CircuitPropertyBean.PropertyEnum.INVOLTAGE.getCode())){
                    circuitPropertyBean.getPowerCircuitList().remove(circuitBean);
                }
            }
        if(circuitPropertyBean.getPowerCircuitList().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前电压等级不符合匹配要求");
        }
    }

    /**
     * 最大功率筛选
     */
    public void maxPowerFilter(CircuitPropertyBean circuitPropertyBean,
                               CircuitPropertyBean.Data data,
                               FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getMaxPower()) || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getPowerCircuitList().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getPowerCircuitList().get(i);
            // 最大功率
            if (!filterPropertyMinValueRange(circuitBean, data.getMaxPower(), CircuitPropertyBean.PropertyEnum.MAXPOWER.getCode())){
                circuitPropertyBean.getPowerCircuitList().remove(circuitBean);
            }
        }
        if(circuitPropertyBean.getPowerCircuitList().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前最大功率不符合匹配要求");
        }
    }

    /**
     * 处理器类型筛选
     */
    public void controllerFilter(CircuitPropertyBean circuitPropertyBean,
                                 CircuitPropertyBean.Data data,
                                 FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getController()) || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getControllerCircuitList().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getControllerCircuitList().get(i);
            // 处理器类型
            if (!filterPropertyEQ(circuitBean, data.getController(), CircuitPropertyBean.PropertyEnum.CONTROLLER.getCode())){
                circuitPropertyBean.getControllerCircuitList().remove(circuitBean);
            }
        }
        if(circuitPropertyBean.getControllerCircuitList().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前处理器类型不符合匹配要求");
        }
    }

    /**
     * 采集类型筛选
     */
    public void signalTypeFilter(CircuitPropertyBean circuitPropertyBean,
                                 CircuitPropertyBean.Data data,
                                 FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getSignalType()) || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            // 采集类型
            if (!filterPropertyEQ(circuitBean, data.getSignalType(), CircuitPropertyBean.PropertyEnum.SIGNALTYPE.getCode())){
                circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
            }
        }
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前采集类型不符合匹配要求");
        }
    }

    /**
     * 最小频率-信号带宽筛选
     */
    public void fMinFilter(CircuitPropertyBean circuitPropertyBean,
                           CircuitPropertyBean.Data data,
                           FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getSignalType())
                || StringUtils.isEmpty(data.getfMin())
                || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            // 最小频率-信号带宽（特殊处理）
            if (!filterPropertyMinValueRange(circuitBean, data.getfMin(), CircuitPropertyBean.PropertyEnum.FMIN.getCode())){
                circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
            }
        }
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前最小频率-信号带宽不符合匹配要求");
        }
    }

    /**
     * 最大频率-信号带宽筛选
     */
    public void fMaxFilter(CircuitPropertyBean circuitPropertyBean,
                           CircuitPropertyBean.Data data,
                           FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getSignalType())
                || StringUtils.isEmpty(data.getfMax())
                || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            // 最大频率-信号带宽（特殊处理）
            if (!filterPropertyMaxValueRange(circuitBean, data.getfMax(), CircuitPropertyBean.PropertyEnum.FMAX.getCode())){
                circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
            }
        }
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前最大频率-信号带宽不符合匹配要求");
        }
    }

    /**
     * 最小输入范围-数据范围筛选
     */
    public void inputMinRangeFilter(CircuitPropertyBean circuitPropertyBean,
                                    CircuitPropertyBean.Data data,
                                    FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getSignalType())
                || StringUtils.isEmpty(data.getInputMinRange())
                || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            // 最小输入范围-数据范围（特殊处理）
            if (!filterPropertyMinValueRange(circuitBean, data.getInputMinRange(), CircuitPropertyBean.PropertyEnum.INPUTMINRANGE.getCode())){
                circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
            }
        }
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前最小输入范围-数据范围不符合匹配要求");
        }
    }

    /**
     * 最大输入范围-数据范围筛选
     */
    public void inputMaxRangeFilter(CircuitPropertyBean circuitPropertyBean,
                                    CircuitPropertyBean.Data data,
                                    FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getSignalType())
                || StringUtils.isEmpty(data.getInputMaxRange())
                || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            // 最大输入范围-数据范围（特殊处理）
            if (!filterPropertyMaxValueRange(circuitBean, data.getInputMaxRange(), CircuitPropertyBean.PropertyEnum.INPUTMAXRANGE.getCode())){
                circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
            }
        }
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前最大输入范围-数据范围不符合匹配要求");
        }
    }

    /**
     * 采样分辨率筛选
     */
    public void adcSamplingBitFilter(CircuitPropertyBean circuitPropertyBean,
                                     CircuitPropertyBean.Data data,
                                     FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getSignalType())
                || StringUtils.isEmpty(data.getAdcSamplingBit())
                || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            // 采样分辨率（特殊处理，为空的不筛选）
            String numberOfDigitsCode = CircuitPropertyBean.PropertyEnum.ADCSAMPLINGBIT.getCode();
            if (circuitBean.getPropertyMap().get(numberOfDigitsCode) != null){
                if (!filterPropertyMaxValueRange(circuitBean, data.getAdcSamplingBit(), numberOfDigitsCode)){
                    circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
                }
            }
        }
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前采样分辨率不符合匹配要求");
        }
    }

    /**
     * 数字信号输入类型筛选
     */
    public void inTypeFilter(CircuitPropertyBean circuitPropertyBean,
                             CircuitPropertyBean.Data data,
                             FilterOutcome filterOutcome){
        if (StringUtils.isEmpty(data.getInType()) || !filterOutcome.bContinue) {
            return;
        }
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size()-1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            // 数字信号输入类型
            if (!filterPropertyEQ(circuitBean, data.getInType(), CircuitPropertyBean.PropertyEnum.INTYPE.getCode())){
                circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
            }
        }
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前数字信号输入类型不符合匹配要求");
        }
    }

    /**
     * 输入信号电压
     */
    public void inputVoltageRangeFilter(CircuitPropertyBean circuitPropertyBean,
                                        CircuitPropertyBean.Data data,
                                        FilterOutcome filterOutcome) {
        if (StringUtils.isEmpty(data.getInputVoltageRange()) || !filterOutcome.bContinue) {
            return;
        }
        // 输入信号电压
        float inputVoltageRange = Float.parseFloat(data.getInputVoltageRange());
        String inputVoltageRangeCode = CircuitPropertyBean.PropertyEnum.INPUTVOLTAGERANGE.getCode();
        List<CircuitPropertyBean.CircuitBean> circuitBeans = new ArrayList<>();
        for (int i = circuitPropertyBean.getSensorCircuitListBean().size() - 1; i >= 0; i--) {
            CircuitPropertyBean.CircuitBean circuitBean = circuitPropertyBean.getSensorCircuitListBean().get(i);
            CircuitPropertyBean.PropertyBean propertyBean = circuitBean.getPropertyMap().get(inputVoltageRangeCode);
            if (propertyBean == null || StringUtils.isEmpty(propertyBean.getValue())) {
                continue;
            }
            if(propertyBean.getValue().contains("-") && inputVoltageRange > 5){
                String[] split = propertyBean.getValue().split("-");
                double[] sDouble = Arrays.stream(split).mapToDouble(Double::parseDouble).toArray();
                if(inputVoltageRange <= sDouble[1]){
                    circuitBeans.add(circuitBean);
                    continue;
                }
            }
            if(!propertyBean.getValue().contains("-") && inputVoltageRange <= 5){
                if(Float.valueOf(propertyBean.getValue()) >= inputVoltageRange){
                    circuitBeans.add(circuitBean);
                }
            }
//            String value = propertyBean.getValue();
//            System.out.println(value);
//            if(inputVoltageRange <= 5 && propertyBean.getValue().contains("-")){
//                circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
//            }
//            if (inputVoltageRange > 5 && !propertyBean.getValue().contains("-")){
//                circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
//            }
//            if (inputVoltageRange > 5 && propertyBean.getValue().contains("-")){
//                String[] split = propertyBean.getValue().split("-");
//                double[] sDouble = Arrays.stream(split).mapToDouble(Double::parseDouble).toArray();
//                if(inputVoltageRange > sDouble[1]){
//                    circuitPropertyBean.getSensorCircuitListBean().remove(circuitBean);
//                }
//            }
        }
        circuitPropertyBean.setSensorCircuitListBean(circuitBeans);
        if(circuitPropertyBean.getSensorCircuitListBean().size() == 0){
            filterOutcome.setBContinue(false);
            filterOutcome.setMsg("page:"+data.getPage()+"\t\t当前输入信号电压不符合匹配要求");
        }

    }

    // -----------------------------过滤方法----------------------------

    /**
     * 过滤相等属性
     */
    public boolean filterPropertyEQ(CircuitPropertyBean.CircuitBean circuitBean, String requestStr, String code){
        // 如果电路属性为空，则删除此电路
        if (circuitBean.getPropertyMap().get(code) == null || circuitBean.getPropertyMap().get(code).getValue() == null){
            return false;
        }

        // 如果前端传来的数据不为空，则继续
        if (StringUtils.isNotEmpty(requestStr)){
            // 如果前端数据和电路属性不一样，则删除此电路
            return requestStr.equals(circuitBean.getPropertyMap().get(code).getValue());
        }

        return true;

    }

    /**
     * 过滤包含属性
     */
    public boolean filterPropertyContain(CircuitPropertyBean.CircuitBean circuitBean, String requestStr, String code){
        // 如果电路属性为空，则删除此电路
        if (circuitBean.getPropertyMap() == null || circuitBean.getPropertyMap().get(code) == null || circuitBean.getPropertyMap().get(code).getValue() == null){
            return false;
        }

        // 如果前端传来的数据不为空，则继续
        if (StringUtils.isNotEmpty(requestStr)){
            // 如果前端数据和电路属性不一样，则删除此电路
            return circuitBean.getPropertyMap().get(code).getValue().contains(requestStr);
        }

        return true;

    }

    /**
     * 过滤质量等级属性
     */
    public boolean filterPropertyQualityGrade(CircuitPropertyBean.CircuitBean circuitBean, String requestStr, String code){
        // 如果电路属性为空，则删除此电路
        if (circuitBean.getPropertyMap().get(code) == null || circuitBean.getPropertyMap().get(code).getValue() == null){
            return false;
        }

        // 如果前端传来的数据不为空，则继续
        if (StringUtils.isNotEmpty(requestStr)){

            String value = circuitBean.getPropertyMap().get(code).getValue();

            switch (requestStr){
                case "Military":
                    if (value.equals("Military")){
                        return true;
                    }
                    break;
                case "Industrial":
                    if (!value.equals("Consumer")){
                        return true;
                    }
                    break;
                case "Consumer":
                    return true;
                default:
                    return false;
            }
        }
        return false;
    }

    /**
     * 过滤最小值范围筛选
     */
    public boolean filterPropertyMinValueRange(CircuitPropertyBean.CircuitBean circuitBean, String requestStr, String code){
        // 如果电路属性为空，则删除此电路
        if (circuitBean.getPropertyMap() == null || circuitBean.getPropertyMap().get(code) == null || circuitBean.getPropertyMap().get(code).getValue() == null){
            return false;
        }

        // 如果前端传来的数据不为空，则继续
        if (StringUtils.isNotEmpty(requestStr)){
            float propertyValue = Float.parseFloat(circuitBean.getPropertyMap().get(code).getValue());
            float inputMin = Float.parseFloat(requestStr);

            return inputMin >= propertyValue;
        }

        return true;

    }

    /**
     * 过滤最大值范围筛选
     */
    public boolean filterPropertyMaxValueRange(CircuitPropertyBean.CircuitBean circuitBean, String requestStr, String code){
        // 如果电路属性为空，则删除此电路
        if (circuitBean.getPropertyMap().get(code) == null || circuitBean.getPropertyMap().get(code).getValue() == null){
            return false;
        }

        // 如果前端传来的数据不为空，则继续
        if (StringUtils.isNotEmpty(requestStr)){
            float propertyValue = Float.parseFloat(circuitBean.getPropertyMap().get(code).getValue());
            float inputMax = Float.parseFloat(requestStr);

            return inputMax <= propertyValue;
        }

        return true;

    }


}
